<template>
 
  <el-card>
    <el-space>
      <el-button v-if="!dataForm.taskId" type="primary" style="margin-top: 12px" @click="createTask()">创建素描作业</el-button>
      <el-button v-if="dataForm.taskId" type="primary" style="margin-top: 12px" @click="createTask()">修改素描作业</el-button>
    </el-space>
    <el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" :label-width="300">
      <el-form-item label="作品主题" prop="taskName">
        <el-input v-model="dataForm.taskName" placeholder="作品主题" style="max-width: 30%;"></el-input>
      </el-form-item>
      <el-form-item label="作业开始时间" prop="startTime">
        <el-date-picker v-model="dataForm.startTime" type="datetime" placeholder="请选择作业开始时间"
          format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss" date-format="MMM DD, YYYY"
          time-format="HH:mm" />
      </el-form-item>
      <el-form-item label="作业结束时间" prop="endTime">
        <el-date-picker v-model="dataForm.endTime" type="datetime" placeholder="请选择作业结束时间" format="YYYY-MM-DD HH:mm:ss"
          value-format="YYYY-MM-DD HH:mm:ss" date-format="MMM DD, YYYY" time-format="HH:mm" />
      </el-form-item>

      <el-form-item label="作业总分" prop="totalScore">
        <el-input v-model="dataForm.totalScore" placeholder="作业总分" style="max-width: 20%;"></el-input>
      </el-form-item>
      <el-form-item label="大模型评分权重" prop="aiWeight">
        <el-input v-model="dataForm.aiWeight" placeholder="大模型评分权重" style="max-width: 20%;"></el-input>
      </el-form-item>
      <el-form-item label="评分要素" prop="scoreElement">
        <el-input v-model="dataForm.scoreElement" style="display: block;max-width: 80%;" :autosize="{ minRows: 2, maxRows: 15 }"
        type="textarea" placeholder="评分要素" />
      </el-form-item>

    </el-form>
  </el-card>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router';
import { ElMessage, ElMessageBox } from 'element-plus'
import { saveSketchTask,getSketchTaskDetail } from '@/api/sm/task'
// 获取作业数据
const router = useRouter()
const route = useRoute()
// 获取路由参数
const taskId = route.query.taskId as string
const courseUserId = route.query.courseUserId as string
// 表单校验规则
const dataRules = ref({
  taskName: [{ required: true, message: '作品主题不能为空', trigger: 'blur' }],
  startTime: [{ required: true, message: '开始时间不能为空', trigger: 'blur' }],
  endTime: [{ required: true, message: '结束时间不能为空', trigger: 'blur' }],
  totalScore: [{ required: true, message: '作品总分不能为空', trigger: 'blur' }],
  aiWeight: [{ required: true, message: '大模型评估权重不能为空', trigger: 'blur' }],
  scoreElement: [{ required: true, message: '评分要素不能为空', trigger: 'blur' }],
})

// 作业表单数据
const dataForm = reactive({
  taskId: taskId,
  courseUserId: courseUserId,
  aiWeight: '',
  totalScore: '',
  taskName: null,
  startTime: '',
  endTime: '',
  scoreElement: '',
  topicId:'',
  taskTopicId:''
})
// 提交修改作业
const createTask = async () => {
  try {
    const payload = {
      ...dataForm
    }
    await saveSketchTask(payload)
    ElMessage.success(dataForm.taskId ? '素描作业更新成功' : '素描作业创建成功')
    router.back()
  } catch (e) {
    console.error("创建素描作业失败")
  }
}

const getSketch = async () => {
  await getSketchTaskDetail(taskId).then((res:any) => {
    dataForm.taskName = res.data.taskName
    dataForm.startTime = res.data.startTime
    dataForm.endTime = res.data.endTime
    dataForm.totalScore = res.data.totalScore
    dataForm.aiWeight = res.data.aiWeight
    dataForm.scoreElement = res.data.scoreElement
    dataForm.topicId = res.data.topicId
    dataForm.taskTopicId = res.data.taskTopicId
  })
}
onMounted(() => {
  if (taskId !== null && taskId !== undefined) {
    getSketch();
  }
});
</script>

<style scoped>
.el-col {
  margin-bottom: 20px;
}

.el-card__header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>